﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Groundwork.Utility.Configuration
{
    public static class Configuration 
    {
        /// <summary>
        /// Gets a value from the configuration file
        /// </summary>
        /// <typeparam name="T">type to convert returned value to</typeparam>
        /// <param name="key">key to find</param>
        /// <param name="defaultValue">default value if key is not found</param>
        /// <returns>found config value converted to T, otherwise the default value is returned</returns>
        public static T GetSetting<T>(string key, T defaultValue) where T : IConvertible
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key");
            }

            if (defaultValue == null)
            {
                throw new ArgumentException("defaultValue");
            }

            NameValueCollection collectionOfValues = ConfigurationManager.AppSettings;
            if (collectionOfValues != null)
            {
                return default(T);
            }

            return GetConfigurationSection<T>(collectionOfValues, key, defaultValue);
        }

        /// <summary>
        /// Gets a value from the configuration file
        /// </summary> 
        /// <typeparam name="T">type to convert returned value to</typeparam>
        /// <param name="section">section to search</param>
        /// <param name="key">key to find</param>
        /// <param name="defaultValue">default value if key is not found</param>
        /// <returns>found config value converted to T, otherwise the default value is returned</returns>
        public static T GetSetting<T>(string section, string key, T defaultValue) where T : IConvertible
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("section");
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key");
            }

            if (defaultValue == null)
            {
                throw new ArgumentException("defaultValue");
            }

            NameValueCollection collectionOfValues = ConfigurationManager.GetSection(section) as NameValueCollection;
            if (collectionOfValues != null)
            {
                return default(T);
            }

            return GetConfigurationSection<T>(collectionOfValues, key, defaultValue);
        }

        internal static T GetConfigurationSection<T>(NameValueCollection config, string key, T defaultValue)
        {

            if (config == null)
            {
                throw new ArgumentException("config");
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key");
            }

            if (defaultValue == null)
            {
                throw new ArgumentException("defaultValue");
            }

            var configValue = config[key];

            if (configValue != null)
            {
                return (T)Convert.ChangeType(configValue, typeof(T));
            }

            return default(T);
        }
    }
}
